package model

import (
	"context"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UmsResourceCategoryModel = (*customUmsResourceCategoryModel)(nil)

type (
	// UmsResourceCategoryModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUmsResourceCategoryModel.
	UmsResourceCategoryModel interface {
		umsResourceCategoryModel
		Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
		RowBuilder() squirrel.SelectBuilder
		CountBuilder(field string) squirrel.SelectBuilder
		FindAll(ctx context.Context, rowbBuilder squirrel.SelectBuilder) ([]*UmsResourceCategory, error)
	}

	customUmsResourceCategoryModel struct {
		*defaultUmsResourceCategoryModel
	}
)

// NewUmsResourceCategoryModel returns a model for the database table.
func NewUmsResourceCategoryModel(conn sqlx.SqlConn, c cache.CacheConf) UmsResourceCategoryModel {
	return &customUmsResourceCategoryModel{
		defaultUmsResourceCategoryModel: newUmsResourceCategoryModel(conn, c),
	}
}

// export logic
func (m *defaultUmsResourceCategoryModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
	return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})

}
func (m *defaultUmsResourceCategoryModel) FindAll(ctx context.Context, rowbBuilder squirrel.SelectBuilder) ([]*UmsResourceCategory, error) {
	rowbBuilder = rowbBuilder.OrderBy("sort asc")
	query, values, err := rowbBuilder.ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*UmsResourceCategory
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return nil, err
	}
}

// export logic
func (m *defaultUmsResourceCategoryModel) RowBuilder() squirrel.SelectBuilder {
	return squirrel.Select(umsResourceCategoryRows).From(m.table)
}

// export logic
func (m *defaultUmsResourceCategoryModel) CountBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("COUNT(" + field + ")").From(m.table)
}

// export logic
func (m *defaultUmsResourceCategoryModel) SumBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("IFNULL(SUM(" + field + "),0)").From(m.table)
}
